﻿/* This application lets you create and modify your Windows calendars, as well as other
 * iCal calendar files, by changing your appointments and adding new appointments. You
 * can also import and export your appointments from/to the current calendar or other
 * calendars.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.Calendar;

namespace C4F.DevKit.Calendar.Samples
{
    public partial class MainForm : Form
    {
        private SelectAppointmentDialog _selectDialog;

        public MainForm()
        {
            InitializeComponent();
        }

        public void RefreshAppointmentList()
        {
            _selectDialog.RefreshList();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            CreateCalendar();
        }

        private void OnOpen(object sender, EventArgs e)
        {
            OpenCalendar();
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void loadCalendarToolStripLabel_ButtonClick(object sender, EventArgs e)
        {
            OpenCalendar();
        }

        private void CreateCalendar()
        {
            try
            {
                calendarService1.CreateCalendar();

                if (string.IsNullOrEmpty(calendarService1.CalendarFilePath))
                {
                    return;
                }

                CloseCalendar();

                _selectDialog = new SelectAppointmentDialog(this.calendarService1);
                _selectDialog.MdiParent = this;
                _selectDialog.Show();
                toolStripStatusLabel.Text = "Created calendar: " + calendarService1.CalendarFilePath;
            }
            catch (Exception)
            {
                MessageBox.Show(
                    String.Format("Error creating new calendar file {0}. Please choose a different file.", calendarService1.CalendarFilePath),
                    "Error while creating new calendar");
            }
        }

        private void OpenCalendar()
        {
            calendarService1.CalendarFilePath = calendarService1.PromptForOpenLocation();

            if (string.IsNullOrEmpty(calendarService1.CalendarFilePath))
            {
                return;
            }

            CloseCalendar();

            try
            {
                _selectDialog = new SelectAppointmentDialog(this.calendarService1);
                _selectDialog.MdiParent = this;
                _selectDialog.Show();
                toolStripStatusLabel.Text = "Loaded calendar: " + calendarService1.CalendarFilePath;
            }
            catch (Exception)
            {
                MessageBox.Show(
                    String.Format("Could not open calendar {0}. Please choose a different file", calendarService1.CalendarFilePath),
                    "Error while trying to open calendar");
            }
        }

        private void CloseCalendar()
        {
            foreach (Form childForm in this.MdiChildren)
            {
                childForm.Close();
            }

            toolStripStatusLabel.Text = "";
        }

        private void newCalendarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateCalendar();
        }
    }
}
